/***************************************************************************************
 *
 *  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
 *
 *  By downloading, copying, installing or using the software you agree to this license.
 *  If you do not agree to this license, do not download, install, 
 *  copy or use the software.
 *
 *  Copyright (C) 2010-2014, Happytimesoft Corporation, all rights reserved.
 *
 *  Redistribution and use in binary forms, with or without modification, are permitted.
 *
 *  Unless required by applicable law or agreed to in writing, software distributed 
 *  under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
 *  CONDITIONS OF ANY KIND, either express or implied. See the License for the specific
 *  language governing permissions and limitations under the License.
 *
****************************************************************************************/

#include "sys_inc.h"
#include "onvif_ptz.h"
#include "onvif_util.h"

extern ONVIF_CLS g_onvif_cls;


BOOL ptz_GetStatus(ONVIF_PROFILE * p_profile, PTZ_STATUS * p_ptz_status)
{
	p_ptz_status->move_sta = PTZ_STA_IDLE;
	p_ptz_status->zoom_sta = PTZ_STA_IDLE;
	p_ptz_status->pantilt_pos_x = 0;
	p_ptz_status->pantilt_pos_y = 0;
	p_ptz_status->zoom_pos = 0;

	onvif_get_time_str(p_ptz_status->utc_time, sizeof(p_ptz_status->utc_time), 0);	
	
	return TRUE;
}

ONVIF_RET onvif_ContinuousMove(ContinuousMove_REQ * p_ContinuousMove_req)
{
	ONVIF_PROFILE * p_profile = onvif_find_profile(p_ContinuousMove_req->profile_token);
	if (NULL == p_profile)
	{
		return ONVIF_ERR_NO_PROFILE;
	}
	else if (NULL == p_profile->ptz_node)
	{
		return ONVIF_ERR_NO_PTZ_PROFILE;
	}

	// do continuous move ... 
	
    return ONVIF_OK;
}

ONVIF_RET onvif_PTZ_Stop(PTZ_Stop_REQ * p_Stop_req)
{
	ONVIF_PROFILE * p_profile = onvif_find_profile(p_Stop_req->profile_token);
	if (NULL == p_profile)
	{
		return ONVIF_ERR_NO_PROFILE;
	}
	else if (NULL == p_profile->ptz_node)
	{
		return ONVIF_ERR_NO_PTZ_PROFILE;
	}

	// stop PTZ moving ... 
	
    return ONVIF_OK;
}

ONVIF_RET onvif_AbsoluteMove(AbsoluteMove_REQ * p_AbsoluteMove_req)
{	
	ONVIF_PROFILE * p_profile = onvif_find_profile(p_AbsoluteMove_req->profile_token);
	if (NULL == p_profile)
	{
		return ONVIF_ERR_NO_PROFILE;
	}
	else if (NULL == p_profile->ptz_node)
	{
		return ONVIF_ERR_NO_PTZ_PROFILE;
	}

	if (p_AbsoluteMove_req->pantilt_pos_x > p_profile->ptz_node->abs_pantilt_x.max || 
		p_AbsoluteMove_req->pantilt_pos_y > p_profile->ptz_node->abs_pantilt_y.max || 
		p_AbsoluteMove_req->zoom_pos > p_profile->ptz_node->abs_zoom.max)
	{
		return ONVIF_ERR_INVALID_POSIION;
	}
	
	// do absolute move ...
	
    return ONVIF_OK;
}

ONVIF_RET onvif_RelativeMove(RelativeMove_REQ * p_RelativeMove_req)
{
	ONVIF_PROFILE * p_profile = onvif_find_profile(p_RelativeMove_req->profile_token);
	if (NULL == p_profile)
	{
		return ONVIF_ERR_NO_PROFILE;
	}
	else if (NULL == p_profile->ptz_node)
	{
		return ONVIF_ERR_NO_PTZ_PROFILE;
	}

	// do relative move ...
	
    return ONVIF_OK;
}

ONVIF_RET onvif_SetPreset(SetPreset_REQ * p_SetPreset_req)
{
    PTZ_PRESET * p_preset = NULL;

	ONVIF_PROFILE * p_profile = onvif_find_profile(p_SetPreset_req->profile_token);
	if (NULL == p_profile)
	{
		return ONVIF_ERR_NO_PROFILE;
	}
	else if (NULL == p_profile->ptz_node)
	{
		return ONVIF_ERR_NO_PTZ_PROFILE;
	}
	
    if (p_SetPreset_req->preset_token[0] != '\0')
    {
        p_preset = onvif_find_preset(p_SetPreset_req->profile_token, p_SetPreset_req->preset_token);
        if (NULL == p_preset)
        {
        	return ONVIF_ERR_NO_TOKEN;
        }
    }
    else
    {
        p_preset = onvif_get_idle_preset(p_SetPreset_req->profile_token);
        if (NULL == p_preset)
        {
        	return ONVIF_ERR_TOO_MANY_PRESETS;
        }
    }

    if (p_SetPreset_req->name[0] != '\0')
    {
    	strcpy(p_preset->name, p_SetPreset_req->name);
    }
    else
    {
    	sprintf(p_preset->name, "PRESET_%d", g_onvif_cls.preset_idx);
    	strcpy(p_SetPreset_req->name, p_preset->name);
    	g_onvif_cls.preset_idx++;
    }
    
    if (p_SetPreset_req->preset_token[0] != '\0')
    {
        strcpy(p_preset->token, p_SetPreset_req->preset_token);
    }
    else
    {
        sprintf(p_preset->token, "PRESET_%d", g_onvif_cls.preset_idx);
        strcpy(p_SetPreset_req->preset_token, p_preset->token);
        g_onvif_cls.preset_idx++;
    }

    // get PTZ current position
    p_preset->pantilt_pos_x = 0;
    p_preset->pantilt_pos_y = 0;
    p_preset->zoom_pos = 0;

    p_preset->used_flag = 1;
    
    return ONVIF_OK;
}

ONVIF_RET onvif_RemovePreset(RemovePreset_REQ * p_RemovePreset_req)
{
    ONVIF_PROFILE * p_profile = onvif_find_profile(p_RemovePreset_req->profile_token);
	if (NULL == p_profile)
	{
		return ONVIF_ERR_NO_PROFILE;
	}

	if (NULL == p_profile->ptz_node)
	{
		return ONVIF_ERR_NO_PTZ_PROFILE;
	}

    PTZ_PRESET * p_preset = onvif_find_preset(p_RemovePreset_req->profile_token, p_RemovePreset_req->preset_token);
    if (NULL == p_preset)
    {
        return ONVIF_ERR_NO_TOKEN;
    }

    memset(p_preset, 0, sizeof(PTZ_PRESET));

    return ONVIF_OK;
}

ONVIF_RET onvif_GotoPreset(GotoPreset_REQ * p_GotoPreset_req)
{	
	ONVIF_PROFILE * p_profile = onvif_find_profile(p_GotoPreset_req->profile_token);
	if (NULL == p_profile)
	{
		return ONVIF_ERR_NO_PROFILE;
	}

	if (NULL == p_profile->ptz_node)
	{
		return ONVIF_ERR_NO_PTZ_PROFILE;
	}

    PTZ_PRESET * p_preset = onvif_find_preset(p_GotoPreset_req->profile_token, p_GotoPreset_req->preset_token);
    if (NULL == p_preset)
    {
        return ONVIF_ERR_NO_TOKEN;
    }

    // goto preset ...

    return ONVIF_OK;
}

ONVIF_RET onvif_GotoHomePosition(GotoHomePosition_REQ * p_GotoHomePosition_req)
{
    ONVIF_PROFILE * p_profile = onvif_find_profile(p_GotoHomePosition_req->profile_token);
    if (NULL == p_profile)
    {
        return ONVIF_ERR_NO_PROFILE;
    }

    if (NULL == p_profile->ptz_node)
    {
    	return ONVIF_ERR_NO_PTZ_PROFILE;
    }

    // goto home position ...

    return ONVIF_OK;
}

ONVIF_RET onvif_SetHomePosition(const char * token)
{
	ONVIF_PROFILE * p_profile = onvif_find_profile(token);
    if (NULL == p_profile)
    {
        return ONVIF_ERR_NO_PROFILE;
    }

    if (NULL == p_profile->ptz_node)
    {
    	return ONVIF_ERR_NO_PTZ_PROFILE;
    }

	if (p_profile->ptz_node->fixed_home_pos == 1)
	{
		return ONVIF_ERR_CANNOT_OVERWRITE_HOME;
	}
	
    // set home position ...

    return ONVIF_OK;
}




